package shell.view.components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import shell.view.utils.ImageConvolution;
/**@author Rafael Rocha*/
public class CloseableTab extends JPanel implements MouseMotionListener, MouseListener{
	private static final long serialVersionUID = 1L;
	private boolean updated;
	private String fontFamily;
	private Font font;
	private Image unhoveredImage;
	private Image hoveredImage;
	private boolean closeHovered;
	private int fontStyle;
	private int gap;
	private int fontsize;
	private JTabbedPane pane;
	private Color start;
	private Color end;
	private Color unhoveredStartColor;
	private Color hoveredStartColor;
	private String title;
	private InabitTab tab;
	
	public CloseableTab(JTabbedPane pane, InabitTab tab, String title){
		this.pane = pane;
		this.tab = tab;
		this.title = title;
		fontStyle = Font.PLAIN;
		fontsize = 12;
		
		unhoveredStartColor = new Color(220,220,220);
		hoveredStartColor = new Color(170,220,250);
		
		start = unhoveredStartColor;
		end = Color.WHITE;
		
		fontFamily = "Helvetica";
		gap = 5;
		font = new Font(fontFamily, fontStyle, fontsize); 
		FontMetrics fm = getFontMetrics(font);
		unhoveredImage = new ImageIcon("rsc//close_transparent.png").getImage();
		hoveredImage = new ImageIcon("rsc//close_selected_transparent.png").getImage();
		setPreferredSize(new Dimension(fm.stringWidth(title) + unhoveredImage.getWidth(null) + 3*gap, fm.getHeight() + gap));
		addMouseMotionListener(this);
		addMouseListener(this);
		//setPreferredSize(new Dimension(120,20));
	}
	@Override
	public void paintComponent(Graphics g2){
		super.paintComponent(g2); 
		BufferedImage bImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
		Graphics g = bImage.getGraphics();
		Color c1 = start;
		Color c2 = end;
		
		int index = pane.indexOfTabComponent(this);
		if(index != -1){
			if(pane.getSelectedIndex() != index){
				c1 = new Color(
						(int)(c1.getRed()*0.8),
						(int)(c1.getGreen()*0.8),
						(int)(c1.getBlue()*0.8));
				c2 = new Color(
						(int)(c2.getRed()*0.8),
						(int)(c2.getGreen()*0.8),
						(int)(c2.getBlue()*0.8));
			}
		}
		
		g.setFont(font);
		int imWidth = unhoveredImage.getWidth(null);
		int width = getWidth() - imWidth - gap - gap;
		FontMetrics fm = getFontMetrics(font);
		int height = fm.getHeight();
		int interations = width;
		
		Color c = c1;
		
		int dr = c2.getRed() - c1.getRed();
		int dg = c2.getGreen() - c1.getGreen();
		int db = c2.getBlue() - c1.getBlue();
		int dw = width/interations;
		for(int i = 0; i < interations; i++) {
			int k = i+1;
			c = new Color(
					(int)(c1.getRed() + k*dr/(double)interations),
					(int)(c1.getGreen() + k*dg/(double)interations),
					(int)(c1.getBlue() + k*db/(double)interations)
					);
			g.setColor(c);
			g.fillRect(i*dw, 0, (i+1)*dw, getHeight());
		}
		g.setColor(Color.BLACK);
		g.drawString(title, gap, height-1);
		drawCloseButton(g);
		
		int[][] matrix = {
		{0,1,0},
		{-1,5,1},
		{0,-1,0}};
		
		g2.drawImage(ImageConvolution.getConvolution(bImage, matrix), 0, 0, getWidth(), getHeight(), null);
		
	}
	
	
	private void drawCloseButton(Graphics g){
		int index = pane.indexOfTabComponent(this);
		Color c = end;
		if(index != -1){
			if(pane.getSelectedIndex() != index){
				c = new Color(
						(int)(c.getRed()*0.8),
						(int)(c.getGreen()*0.8),
						(int)(c.getBlue()*0.8));
			}
		}
		g.setFont(font);
		FontMetrics fm = getFontMetrics(font);
		int height = fm.getHeight();
		int imWidth = unhoveredImage.getWidth(null);
		
		int x = getWidth() - imWidth - gap;
		int y = height - imWidth;
		g.setColor(c);
		g.fillRect(x, 0, getWidth() - x, getHeight());
		if(!closeHovered){
			g.drawImage(unhoveredImage, x, y, null);
		}else {
			g.drawImage(hoveredImage, x, y, null);
		}
	}
	public boolean over(int px, int py){
		FontMetrics fm = getFontMetrics(font);
		int height = fm.getHeight();
		int imWidth = unhoveredImage.getWidth(null);
		int imHeight = unhoveredImage.getHeight(null);
		
		int x = getWidth() - imWidth - gap;
		int y = height - imWidth;
		int cx = x  + imWidth/2;
		int cy= y + imHeight/2;
		
		int dx = px - cx;
		int dy = py - cy;
		
		double distance = Math.sqrt(dx*dx + dy*dy);
		return distance < imWidth/2;
		
	}
	public void setHovered(boolean hovered){
		this.closeHovered = hovered;
		repaint();
	}
	@Override
	public void mouseDragged(MouseEvent arg0) {
		
	}
	@Override
	public void mouseMoved(MouseEvent p) {
		int x = p.getX(), y = p.getY();
		
		setHovered(over(x, y));
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		
	}
	@Override
	public void mouseEntered(MouseEvent p) {
		int x = p.getX(), y = p.getY();
		setHovered(over(x, y));
		start = hoveredStartColor;
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		setHovered(false);
		start = unhoveredStartColor;
	}
	@Override
	public void mousePressed(MouseEvent m) {
		
		int index = pane.indexOfTabComponent(this);
		if(index != -1){
			if(m.getButton() == MouseEvent.BUTTON2) {
				pane.remove(index);
			}else {
				pane.setSelectedIndex(index);
				if(!updated) {
					tab.update();
					tab.repaint();
					updated =  true;
				}
				if(closeHovered){
					pane.remove(index);
				}
			}
		}
		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		updated = false;
	}
	
}
